package bb.lanxing.lib.widget;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class ScrollTabStrip extends BaseScrollTabStrip {
    private static final String TAG = "ScrollTabStrip";
    private final DataSetObserver dataSetObserver;
    private final PageListener pageListener;
    private ViewPager viewPager;

    public ScrollTabStrip(Context context) {
        super(context);
        this.pageListener = new PageListener();
        this.dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                ScrollTabStrip.this.notifyAllTabs();
            }
        };
    }

    public ScrollTabStrip(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.pageListener = new PageListener();
        this.dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                ScrollTabStrip.this.notifyAllTabs();
            }
        };
    }

    public ScrollTabStrip(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.pageListener = new PageListener();
        this.dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                ScrollTabStrip.this.notifyAllTabs();
            }
        };
    }

    public ScrollTabStrip(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
        this.pageListener = new PageListener();
        this.dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                ScrollTabStrip.this.notifyAllTabs();
            }
        };
    }

    @Override
    protected void onTabClick(int i) {
        ViewPager viewPager = this.viewPager;
        if (viewPager != null) {
            viewPager.setCurrentItem(i, true);
        }
    }

    public ViewPager getViewPager() {
        return this.viewPager;
    }

    public void setViewPager(ViewPager viewPager) {
        ViewPager viewPager2 = this.viewPager;
        if (viewPager2 != null) {
            viewPager2.getAdapter().unregisterDataSetObserver(this.dataSetObserver);
            this.viewPager.removeOnPageChangeListener(this.pageListener);
        }
        this.viewPager = viewPager;
        this.currentPosition = viewPager.getCurrentItem();
        PagerAdapter adapter = viewPager.getAdapter();
        if (adapter != null) {
            int count = adapter.getCount();
            this.titles = new String[count];
            for (int i = 0; i < count; i++) {
                this.titles[i] = adapter.getPageTitle(i).toString();
            }
            notifyAllTabs();
            adapter.registerDataSetObserver(this.dataSetObserver);
            viewPager.addOnPageChangeListener(this.pageListener);
            return;
        }
        throw new IllegalStateException("ViewPager not set PagerAdapter yet !");
    }

    @Override
    public void notifyAllTabs() {
        PagerAdapter adapter = this.viewPager.getAdapter();
        if (adapter != null) {
            int count = adapter.getCount();
            this.titles = new String[count];
            for (int i = 0; i < count; i++) {
                this.titles[i] = adapter.getPageTitle(i).toString();
            }
        }
        super.notifyAllTabs();
    }

    public void setRedDot(int i, boolean z) {
        if (this.redDotIndex == null || this.redDotIndex.length != this.tabCount) {
            this.redDotIndex = new boolean[this.tabCount];
        }
        this.redDotIndex[i] = z;
        invalidate();
    }

    private class PageListener implements ViewPager.OnPageChangeListener {
        private int tabIndex;

        @Override
        public void onPageScrollStateChanged(int i) {
        }

        @Override
        public void onPageSelected(int i) {
        }

        private PageListener() {
            this.tabIndex = ScrollTabStrip.this.currentPosition;
        }

        @Override
        public void onPageScrolled(int i, float f, int i2) {
            int round = Math.round(f) + i;
            int i3 = this.tabIndex;
            if (i3 != round) {
                ScrollTabStrip.this.switchTabTextColor(i3, round);
                this.tabIndex = round;
            }
            ScrollTabStrip.this.currentPosition = i;
            ScrollTabStrip.this.currentPositionOffset = f;
            ScrollTabStrip scrollTabStrip = ScrollTabStrip.this;
            scrollTabStrip.scrollToChild(i, (int) (f * scrollTabStrip.tabsContainer.getChildAt(i).getWidth()));
            ScrollTabStrip.this.invalidate();
        }
    }
}
